<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="sn码" prop="snCode" label-width="120px">
        <el-input
            v-model="queryParams.snCode"
            placeholder="请输入sn码"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品物料ID" prop="itemId" label-width="120px">
        <el-input
            v-model="queryParams.itemId"
            placeholder="请输入产品物料ID"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编码" prop="itemCode" label-width="120px">
        <el-input
            v-model="queryParams.itemCode"
            placeholder="请输入物料编码"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="itemName" label-width="120px">
        <el-input
            v-model="queryParams.itemName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品检测单主键" prop="qcProductId" label-width="120px">
        <el-input
          v-model="queryParams.qcProductId"
          placeholder="请输入产品检测单主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产工单ID" prop="workorderId" label-width="120px">
        <el-input
          v-model="queryParams.workorderId"
          placeholder="请输入生产工单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存记录ID" prop="materialStockId" label-width="120px">
        <el-input
          v-model="queryParams.materialStockId"
          placeholder="请输入库存记录ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="SN码批次号" prop="snGenBatchCode" label-width="120px">
        <el-input
            v-model="queryParams.snGenBatchCode"
            placeholder="请输入SN码批次号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="当前所在库区ID" prop="locationId" label-width="120px">
        <el-input
          v-model="queryParams.locationId"
          placeholder="请输入当前所在库区ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前所在库位ID" prop="areaId" label-width="120px">
        <el-input
          v-model="queryParams.areaId"
          placeholder="请输入当前所在库位ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产批次号" prop="productBatchCode" label-width="120px">
        <el-input
          v-model="queryParams.productBatchCode"
          placeholder="请输入生产批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="入库批次号" prop="stockInBatchCode" label-width="120px">
        <el-input
          v-model="queryParams.stockInBatchCode"
          placeholder="请输入入库批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售出库行ID" prop="salseLineId" label-width="120px">
        <el-input
          v-model="queryParams.salseLineId"
          placeholder="请输入销售出库行ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品返回记录ID" prop="returnRecordId" label-width="120px">
        <el-input
          v-model="queryParams.returnRecordId"
          placeholder="请输入产品返回记录ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品维修记录ID" prop="repairRecordId" label-width="120px">
        <el-input
          v-model="queryParams.repairRecordId"
          placeholder="请输入产品维修记录ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="bom版本主键" prop="bomVersionId" label-width="120px">
        <el-input
          v-model="queryParams.bomVersionId"
          placeholder="请输入bom版本主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="bom版本名字" prop="bomVersion" label-width="120px">
        <el-input
          v-model="queryParams.bomVersion"
          placeholder="请输入bom版本名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:wm:sntrace:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:wm:sntrace:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:wm:sntrace:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:wm:sntrace:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="snId" >
        <template slot-scope="scope">
          <el-button type="text" @click="handleView(scope.row)"
                     v-hasPermi="['mes:wm:sntrace:query']">{{ scope.row.snId }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="sn码" align="center" prop="snCode"  width="120"/>
      <el-table-column label="产品检测单ID" align="center" prop="qcProductId" width="120"/>
      <el-table-column label="生产工单ID" align="center" prop="workorderId" width="120"/>
      <el-table-column label="库存记录ID" align="center" prop="materialStockId" width="120"/>
      <el-table-column label="产品物料ID" align="center" prop="itemId" width="120"/>
      <el-table-column label="物料编码" align="center" prop="itemCode" />
      <el-table-column label="物料名称" align="center" prop="itemName" />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" width="120"/>
      <el-table-column label="库区ID" align="center" prop="locationId" width="120"/>
      <el-table-column label="库位ID" align="center" prop="areaId" width="120"/>
      <el-table-column label="生产批次号" align="center" prop="productBatchCode" width="120"/>
      <el-table-column label="SN码批次号" align="center" prop="snGenBatchCode" width="120"/>
      <el-table-column label="入库批次号" align="center" prop="stockInBatchCode" width="90"/>
      <el-table-column label="销售出库行ID" align="center" prop="salseLineId" width="120"/>
      <el-table-column label="产品返回记录ID" align="center" prop="returnRecordId" width="120"/>
      <el-table-column label="产品维修记录ID" align="center" prop="repairRecordId" width="120"/>
      <el-table-column label="bom版本主键" align="center" prop="bomVersionId" width="120"/>
      <el-table-column label="bom版本名字" align="center" prop="bomVersion" width="120"/>
      <el-table-column label="产品检验结果" align="center" prop="checkResult" width="120"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:wm:sntrace:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:wm:sntrace:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改sn关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col span="12">
            <el-form-item label="产品检测单主键" prop="qcProductId" label-width="120px">
              <el-input v-model="form.qcProductId" placeholder="请输入产品检测单主键" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="生产工单ID" prop="workorderId" label-width="120px">
              <el-input v-model="form.workorderId" placeholder="请输入生产工单ID" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="12">
            <el-form-item label="库存记录ID" prop="materialStockId" label-width="120px">
              <el-input v-model="form.materialStockId" placeholder="请输入库存记录ID" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="产品物料ID" prop="itemId" label-width="120px">
              <el-input v-model="form.itemId" placeholder="请输入产品物料ID" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="12">
            <el-form-item label="物料编码" prop="itemCode" label-width="120px">
              <el-input v-model="form.itemCode" placeholder="请输入物料编码" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="物料名称" prop="itemName" label-width="120px">
              <el-input v-model="form.itemName" placeholder="请输入物料名称" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="12">
            <el-form-item label="规格" prop="specification" label-width="120px">
              <el-input v-model="form.specification" placeholder="请输入规格" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="单位" prop="unitOfMeasure" label-width="120px">
              <el-input v-model="form.unitOfMeasure" placeholder="请输入单位" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="12">
            <el-form-item label="sn码" prop="snCode" label-width="120px">
              <el-input v-model="form.snCode" placeholder="请输入sn码" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="状态" label-width="120px">
              <el-input v-model="form.status" placeholder="状态" :disabled="!isEdit"/>
<!--              <el-radio-group v-model="form.status" :disabled="!isEdit">
                <el-radio label="1">请选择字典生成</el-radio>
              </el-radio-group>-->
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="12">
            <el-form-item label="当前所在库区ID" prop="locationId" label-width="120px">
              <el-input v-model="form.locationId" placeholder="请输入当前所在库区ID" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="当前所在库位ID" prop="areaId" label-width="120px">
              <el-input v-model="form.areaId" placeholder="请输入当前所在库位ID" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="12">
            <el-form-item label="生产批次号" prop="productBatchCode" label-width="120px">
              <el-input v-model="form.productBatchCode" placeholder="请输入生产批次号" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="SN码批次号" prop="snGenBatchCode" label-width="120px">
              <el-input v-model="form.snGenBatchCode" placeholder="请输入SN码批次号" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="入库批次号" prop="stockInBatchCode" label-width="120px">
              <el-input v-model="form.stockInBatchCode" placeholder="请输入入库批次号" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="销售出库行ID" prop="salseLineId" label-width="120px">
              <el-input v-model="form.salseLineId" placeholder="请输入销售出库行ID" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="12">
            <el-form-item label="产品返回记录ID" prop="returnRecordId" label-width="120px">
              <el-input v-model="form.returnRecordId" placeholder="请输入产品返回记录ID" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="产品维修记录ID" prop="repairRecordId" label-width="120px">
              <el-input v-model="form.repairRecordId" placeholder="请输入产品维修记录ID" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>

          <el-col span="12">
            <el-form-item label="bom版本主键" prop="bomVersionId" label-width="120px">
              <el-input v-model="form.bomVersionId" placeholder="请输入bom版本主键" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>

          <el-col span="12">
            <el-form-item label="bom版本名字" prop="bomVersion" label-width="120px">
              <el-input v-model="form.bomVersion" placeholder="请输入bom版本名字" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="产品检测结果" prop="checkResult" label-width="120px">
              <el-input v-model="form.checkResult" placeholder="产品检测结果" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="24">
            <el-form-item label="备注" prop="remark" label-width="120px">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="!isEdit"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInventory, getInventory, delInventory, addInventory, updateInventory } from "@/api/mes/wm/sninventory";

export default {
  name: "Inventory",
  data() {
    return {
      isEdit:true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // sn关联表格数据
      inventoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qcProductId: null,        workorderId: null,        materialStockId: null,        itemId: null,        itemCode: null,        itemName: null,        specification: null,        unitOfMeasure: null,        snCode: null,        locationId: null,        areaId: null,        productBatchCode: null,        snGenBatchCode: null,        stockInBatchCode: null,        salseLineId: null,        returnRecordId: null,        repairRecordId: null,        bomVersionId: null,        bomVersion: null      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workorderId: [
          { required: true, message: "生产工单ID不能为空", trigger: "blur" }
        ],        itemId: [
          { required: true, message: "产品物料ID不能为空", trigger: "blur" }
        ],        snCode: [
          { required: true, message: "sn码不能为空", trigger: "blur" }
        ],      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询sn关联列表 */
    getList() {
      this.loading = true;
      this.isEdit=true;
      listInventory(this.queryParams).then(response => {
        this.inventoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 查询明细按钮操作
    handleView(row) {
      this.reset();
      const snId = row.snId;
      this.isEdit=false;
      getInventory(snId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看详细信息";
        this.optType = "view";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        snId: null,        qcProductId: null,        workorderId: null,        materialStockId: null,        itemId: null,        itemCode: null,        itemName: null,        specification: null,        unitOfMeasure: null,        snCode: null,        status: "0",        createTime: null,        updateTime: null,        locationId: null,        areaId: null,        productBatchCode: null,        snGenBatchCode: null,        stockInBatchCode: null,        salseLineId: null,        returnRecordId: null,        repairRecordId: null,        remark: null,        deleted: null,        deleteAt: null,        version: null,        bomVersionId: null,        bomVersion: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.snId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isEdit=true;
      this.open = true;
      this.title = "添加sn关联";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isEdit=true;
      const snId = row.snId || this.ids
      getInventory(snId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改sn关联";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.snId != null) {
            updateInventory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const snIds = row.snId || this.ids;
      this.$modal.confirm('是否确认删除sn关联编号为"' + snIds + '"的数据项？').then(function() {
        return delInventory(snIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/pro/sninventory/export', {
        ...this.queryParams
      }, `inventory_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
